< previous page page_65 next page >

Page 65
Useful API Functions
There are two reasons you might want to use an API function: It does something Visual Basic cannot do, or it does something faster than Visual Basic can do it. For example, the Polyline API function can draw a series of connected lines much faster than Visual Basic's Line method can. Unfortunately, API routines define screen coordinates in a slightly different way from Visual Basic. The next section describes ways you can deal with this problem. The sections that follow describe a few API functions that are useful in graphics programming. Some of these functions are used extensively in later parts of the book, and they are described in more detail there.
Drawing
Visual Basic's drawing methods are fairly good at what they do. They allow you to draw simple shapes quickly and easily. The Point and PSet methods give you complete control over every pixel in an image.
Visual Basic's ScaleMode properties also let you work in one of several different coordinate systems, including custom coordinate systems you define yourself. When you use API functions, you can work in only one coordinate system: pixels. This limitation can be extremely inconvenient. Your application may not translate naturally into pixels. In addition, not all computer monitors have the same number of pixels per inch vertically and horizontally. As a result, writing a program that produces the same results on all computer screens can be difficult.
One way to work around this problem is to convert your coordinates from whatever unit is convenient into pixels using the ScaleX and ScaleY methods. The following code illustrates this approach:
e10a6a0a59f035e870d90cca05b85fce.gif
pixel_x = pic.ScaleX(x - pic.ScaleLeft, _
    pic.ScaleMode, vbPixels)
pixel_y = pic.ScaleY(y - pic.ScaleTop, -
    pic.ScaleMode, vbPixels)
If you need to convert lots of coordinates many times, you can make this code a bit faster by storing the values of ScaleLeft and ScaleTop and by precomputing some scaling factors.
e10a6a0a59f035e870d90cca05b85fce.gif
Dim scale_left As Single
Dim scale_top As Single
Dim pix_per_unit_x As Single
Dim pix_per_unit_y As Single

    ' Prepare for translation.
    scale_left = pic.ScaleX(pic.ScaleLeft, _
        pic.ScaleMode, vbPixels)

 
< previous page page_65 next page >